const eventClass = require('./eventClass');

const {
    dialog,
} = eval('require')('electron');

const getFileDefaultParam = {
    title: '选择文件',
    defaultPath: null,
    buttonLabel: '确认',
    filters: [{ name: '任意文件', extensions: ['*'] }],
    properties: ['openFile'],
};

const getDirDefaultParam = {
    title: '选择文件夹',
    defaultPath: null,
    buttonLabel: '确定',
    properties: ['openDirectory'],
};

const getSaveFileDefaultParam = Object.assign({}, getFileDefaultParam, {
    showsTagField: true,
    nameFieldLabel: 'electron-save',
    title: '创建文件',
});

const getSaveDirDefaultParam = Object.assign({}, getDirDefaultParam, {
    showsTagField: true,
    nameFieldLabel: 'electron-save',
    securityScopedBookmarks: true,
    title: '创建文件夹',
});

class dialogClass extends eventClass {
    constructor(win) {
        super('dialog');
        this.win = win;
    }

    getFile(cb, obj = { title, defaultPath, buttonLabel, filters }) {
        obj = Object.assign({}, getFileDefaultParam, obj);
        dialog.showOpenDialog(this.win, obj, (data) => {
            this.returnMethod(cb, { data: data || false });
        });
    }

    getDir(cb, obj = { title, defaultPath, buttonLabel }) {
        obj = Object.assign({}, getDirDefaultParam, obj);
        dialog.showOpenDialog(this.win, obj, (data) => {
            this.returnMethod(cb, { data: data || false });
        });
    }

    getSaveFile(cb, obj = { title, defaultPath, buttonLabel, filters }) {
        obj = Object.assign({}, getSaveFileDefaultParam, obj);
        dialog.showSaveDialog(this.win, obj, (data) => {
            this.returnMethod(cb, { data: data || false });
        });
    }

    getSaveDir(cb, obj = { title, defaultPath, buttonLabel }) {
        obj = Object.assign({}, getSaveDirDefaultParam, obj);
        dialog.showSaveDialog(this.win, obj, (data) => {
            this.returnMethod(cb, { data: data || false });
        });
    }
}

module.exports = dialogClass;
